/*
 * @Author: herw
 * @Date: 2021-01-14 13:48:58
 * @Last Modified by: herw
 * @Last Modified time: 2021-04-12 16:54:12
 */
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          clearable
          placeholder="模糊搜索"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission">
        <template slot="right">
          <el-button
            v-permission="['admin', 'task:edit']"
            :disabled="!selectIndex"
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-upload"
            @click="check"
          >审核
          </el-button>
        </template>
      </crudOperation>
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      highlight-current-row
      stripe
      style="width: 100%"
      @selection-change="handleCurrentChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="Topic Name" />
      <el-table-column width="130" prop="approved" label="Approval Status" />
      <el-table-column prop="comments" label="Comments" />
      <el-table-column
        v-if="checkPermission(['admin', 'task:edit', 'task:del'])"
        label="操作"
        width="150px"
        align="center"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="570px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        :rules="rules"
        size="small"
        label-width="116px"
      >
        <el-form-item label="Topic Name" prop="name">
          <el-input v-model="form.name" style="width: 370px" />
        </el-form-item>
        <el-form-item label="Team Name" prop="roleId">
          <el-select
            v-model="form.roleId"
            style="width: 370px"
            placeholder="请选择"
          >
            <el-option
              v-for="item in roles"
              :key="item.name"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Location" prop="location">
          <el-select
            v-model="form.location"
            style="width: 370px"
            placeholder="请选择数据范围"
          >
            <el-option
              v-for="item in locations"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Message Rate" prop="messageRate">
          <el-input-number v-model.number="form.messageRate" :min="1" controls-position="right" style="width: 145px;" /> (message/s)
        </el-form-item>
        <el-form-item label="Partitions" prop="partition">
          <el-input-number v-model.number="form.partition" :min="1" controls-position="right" style="width: 145px;" />
        </el-form-item>
        <el-form-item label="Replication Factor" prop="replication">
          <el-input-number v-model.number="form.replication" :min="1" controls-position="right" style="width: 145px;" />
        </el-form-item>
        <el-form-item label="TTL" prop="ttl">
          <el-input-number v-model.number="form.ttl" :min="1" controls-position="right" style="width: 145px;" /> (H)
        </el-form-item>
        <el-form-item label="描述信息" prop="comments">
          <el-input v-model="form.comments" style="width: 370px;" rows="5" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >确认</el-button>
      </div>
    </el-dialog>
    <!-- 审核表单 -->
    <tForm ref="checkTopic" :key="times" :topic="topic" />
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudTopicTask from '@/api/mq/kafka/topicTask'
import { getAll } from '@/api/system/role'
import tForm from './checkTopic'

const defaultForm = { }
export default {
  name: 'TopicTask',
  components: { tForm, pagination, crudOperation, rrOperation, udOperation },
  cruds() {
    return CRUD({ title: '集群', url: 'api/mq/topic/task', crudMethod: { ...crudTopicTask }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      locations: ['dev', 'prod'],
      roles: [],
      selectIndex: '',
      times: 0,
      currentRow: {},
      topic: {},
      permission: {
        add: ['admin', 'task:add'],
        edit: ['admin', 'task:edit'],
        del: ['admin', 'task:del']
      },
      rules: {
        name: [
          { required: true, message: '请输入集群名', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.selectIndex = ''
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.getRoles()
    },
    // 获取弹窗内角色数据
    getRoles() {
      getAll().then(res => {
        this.roles = res
      }).catch(() => { })
    },
    check() {
      this.$refs.checkTopic.dialog = true
      // this.$refs.checkTopic.topic = this.currentRow
    },
    handleCurrentChange(selection) {
      this.crud.selections = selection
      if (selection.length === 1) {
        const row = selection[0]
        this.selectIndex = row.id
        this.currentRow = row
        this.topic = row
        this.times = this.times + 1
      } else {
        this.currentRow = {}
        this.topic = {}
        this.selectIndex = ''
      }
    }
  }
}
</script>
